package com.hxd.otc.module.coin_deal.model;

import com.hxd.otc.api.ICoinDealApi;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.coin_deal.bean.ChooseCoinBean;
import com.hxd.otc.module.coin_deal.bean.CoinDealRecoderBean;
import com.hxd.otc.module.coin_deal.bean.CoinDealRequestBean;
import com.hxd.otc.module.coin_deal.bean.CoinDealResultBean;
import com.hxd.otc.module.coin_deal.bean.CoinRequestTransBean;
import com.hxd.otc.module.coin_deal.bean.DialogCoinTypeBean;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.net.RetrofitManager;
import com.hxd.otc.utils.EncryptUtils;
import com.uber.autodispose.AutoDisposeConverter;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by lzy on 2018/7/9 0009.
 * description ${币币交易model}
 */
public class CoinDealModel {
    private CoinDealModel() {
    }

    private static class SingletonHolder {
        private static final CoinDealModel instance = new CoinDealModel();
    }

    public static CoinDealModel getInstance() {
        return SingletonHolder.instance;
    }

    /**
     * 获取币币交易列表
     *
     * @param bean
     * @param observer
     */
    public void loadData(CoinDealRequestBean bean, BaseObserver<CoinDealResultBean> observer,
                         AutoDisposeConverter<BaseBean<CoinDealResultBean>> converter) {
        RetrofitManager.getDefault().create(ICoinDealApi.class)
                .coinDealList(bean.type, bean.bbTransPair, bean.pageNo, bean.pageSize)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 最近成交价
     */
    public void lastPrice(String bbTransPair, BaseObserver observer, AutoDisposeConverter<BaseBean> converter) {
        RetrofitManager.getDefault().create(ICoinDealApi.class)
                .lastPrice(bbTransPair)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 最近成交价
     */
    public void getCoinTypePrice(BaseObserver observer, AutoDisposeConverter<BaseBean<List<ChooseCoinBean>>> converter) {
        RetrofitManager.getDefault().create(ICoinDealApi.class)
                .getCoinTypePrice()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }


    /**
     * 获取我的挂单列表
     *
     * @param observer
     * @param converter
     */
    public void getMyEntryOrders(int pageNo, int pageSize, BaseObserver<CoinDealResultBean> observer,
                                 AutoDisposeConverter<BaseBean<CoinDealResultBean>> converter) {
        getCoinDealApi().getMyEntryOrders(pageNo, pageSize)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * * 新增挂单
     *
     * @param price     单价
     * @param amount    数量
     * @param transPair 交易对
     * @param password  交易密码
     */
    public void insertEntryOrders( String price, String amount, String transPair, String password,
                                  BaseObserver<String> observer, AutoDisposeConverter<BaseBean<String>> converter) {

        getCoinDealApi().insertEntryOrders(price, amount, transPair, EncryptUtils.getRSA(password))
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 删除挂单
     *
     * @param id 挂单id
     */
    public void deleteEntryOrders(String id, BaseObserver<String> observer,
                                  AutoDisposeConverter<BaseBean<String>> converter) {
        getCoinDealApi().deleteEntryOrders(id)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 获取币币交易记录
     */
    public void getCoinDealTransactionRecord(BaseObserver<CoinDealRecoderBean> observer,
                                             AutoDisposeConverter<BaseBean<CoinDealRecoderBean>> converter, int... category) {

        getCoinDealApi().getCoinDealTransactionRecord(category[0], category[1])
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    private ICoinDealApi getCoinDealApi() {
        return RetrofitManager.getDefault().create(ICoinDealApi.class);
    }

    public void transCoinDeal(CoinRequestTransBean bean, BaseObserver observer, AutoDisposeConverter<BaseBean> converter) {
        getCoinDealApi().trans(bean.bbAdvertId, bean.transAmount, bean.password)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);

    }

    /**
     * 最近成交价
     */
    public void getCoinType(BaseObserver observer, AutoDisposeConverter<BaseBean<List<DialogCoinTypeBean>>> converter) {
        RetrofitManager.getDefault().create(ICoinDealApi.class)
                .getCoinType()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }
}
